//
//  HomeHDVC.m
//  jingdongfang
//
//  Created by LJL on 2018/5/9.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeHDVC.h"
#import "HomeHDTypeCell.h"
#import "IGEmptyView.h"
#import "HomeHDFootReusableView.h"
#import "HomeHDHeaderReusableView.h"
#import "HomeHDCell.h"
#import "IGHomeHDPopularTypeApi.h"
#import "IGHomeHDFineApi.h"
#import "IGPictureListDetailVC.h"

@interface HomeHDVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    NSString *pageNumber;
    NSString *pageSize;
    NSString *queryTime;//查询时间
}
@property (nonatomic, strong) UICollectionView *collectionV;
@property (nonatomic, strong) IGHomeHDPopularTypeApi *api;
@property (nonatomic, strong) IGHomeHDFineApi *fineApi;

@property (nonatomic, strong) NSMutableArray *popularTypeArr;
@property (nonatomic, strong) NSMutableArray *hdArr;
@property (nonatomic, strong) NSString *type;
@end

@implementation HomeHDVC

-(UICollectionView *)collectionV
{
    if (!_collectionV)
    {
        _collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
        _collectionV.dataSource = self;
        _collectionV.delegate = self;
        _collectionV.backgroundColor = [UIColor c3Color];
        [_collectionV registerClass:[HomeHDTypeCell class] forCellWithReuseIdentifier:@"HomeHDTypeCell"];
        [self.collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableViewHeader"];
        [_collectionV registerNib:[UINib nibWithNibName:@"HomeHDFootReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"HomeHDFootReusableView"];
        [_collectionV registerNib:[UINib nibWithNibName:@"HomeHDHeaderReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeHDHeaderReusableView"];
        [_collectionV registerClass:[HomeHDCell class] forCellWithReuseIdentifier:@"HomeHDCell"];

        __weak typeof(self) weakSelf = self;
        [_collectionV ig_setHeaderWithRefreshingBlock:^{
            pageNumber = @"0";
            [weakSelf dataRequest];
        }];
    }
    return _collectionV;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"home12", nil);
    [self.view addSubview:self.collectionV];
    [self initBaseData];
    [self dataRequest];
}

- (void)initBaseData
{
    self.api = [[IGHomeHDPopularTypeApi alloc] init];
    self.fineApi = [[IGHomeHDFineApi alloc] init];
    pageNumber = @"0";
    pageSize = @"10";
    queryTime = @"";
    self.type = @"1";
    self.popularTypeArr = [@[] mutableCopy];
    self.hdArr = [@[] mutableCopy];
}

- (void)dataRequest
{
    [self.api startWithDecodeClass:[IGHomeHDPopularTypeResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [_collectionV ig_endHeaderRefreshing];
        [_collectionV ig_endFooterRefreshing];
        if (responseObject)
        {
            IGHomeHDPopularTypeResp *resp = responseObject;
            IGHomeHDPopularTypeModel *model = resp.data;
            if (model.lstLabels.count == 11)
            {
                _popularTypeArr = [NSMutableArray arrayWithArray:model.lstLabels];
            }
            [_collectionV reloadData];
        }
    }];
    
    [self dataRequest1];
}

- (void)dataRequest1
{
    @KWeakObj(self)
    self.fineApi.type = _type;
    self.fineApi.pageNumber = pageNumber;
    self.fineApi.pageSize = pageSize;
    self.fineApi.queryTime = queryTime;
    [self.fineApi startWithDecodeClass:[IGHomeHDFineResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [_collectionV ig_endHeaderRefreshing];
        [_collectionV ig_endFooterRefreshing];
        if (responseObject)
        {
            IGHomeHDFineResp *resp = responseObject;
            IGHomeHDFineModel *model = resp.data;
            if (model.lstDrawlist && model.lstDrawlist.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [_collectionV ig_endFooterRefreshingWithNoMoreData];
            }
            queryTime = model.time;
            if ([pageNumber isEqualToString:@"0"])
            {
                _hdArr = [NSMutableArray arrayWithArray:model.lstDrawlist];
            }
            else
            {
                [_hdArr addObjectsFromArray:model.lstDrawlist];
            }
            [_collectionV reloadData];
        }
        else
        {
            //空白页处理逻辑写在这里
        }
    }];
}



- (void)setupFooter
{
    @KWeakObj(self)
    [_collectionV ig_setFooterWithRefreshingBlock:^{
        pageNumber = [NSString stringWithFormat:@"%ld", [pageNumber integerValue] + 1];
        [selfWeak dataRequest1];
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return _popularTypeArr.count ? 1 : 0;
    }
    else if (section == 1)
    {
        return _hdArr.count ? : 0;
    }
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return CGSizeMake(KSCREENWIDTH, (KSCREENWIDTH - 20) * 24 / 71);
    }
    else if (indexPath.section == 1)
    {
        return CGSizeMake((KSCREENWIDTH - 5) / 2, (KSCREENWIDTH - 3) / 2 + 38);
    }
    return CGSizeZero;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        HomeHDTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeHDTypeCell" forIndexPath:indexPath];
        cell.typeArr = _popularTypeArr;
        return cell;
    }
    else if (indexPath.section == 1)
    {
        IGDrawModel *hd = _hdArr[indexPath.row];
    HomeHDCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeHDCell" forIndexPath:indexPath];
    cell.isBig = YES;
        cell.hdModel = hd;
    return cell;
    }
    return nil;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    @KWeakObj(self);
    if (kind == UICollectionElementKindSectionHeader)
    {
        if (indexPath.section == 0)
        {
            UICollectionReusableView *resV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableViewHeader" forIndexPath:indexPath];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, resV.width - 20, resV.height)];
            label.text = NSLocalizedString(@"Hot Categories", nil);
            label.textColor = [UIColor c4Color];
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont boldSystemFontOfSize:19];
            [resV addSubview:label];
            return resV;
        }
        else if (indexPath.section == 1)
        {
            HomeHDHeaderReusableView *resV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeHDHeaderReusableView" forIndexPath:indexPath];
            resV.hotBlock = ^(){
                self.type = @"3";
                pageNumber = @"0";
                [selfWeak dataRequest1];
            };
            resV.newsBlock = ^(){
                self.type = @"2";
                pageNumber = @"0"; 
                [selfWeak dataRequest1];
            };
            return resV;
        }
    }
    else if (kind == UICollectionElementKindSectionFooter)
    {
        if (indexPath.section == 0)
        {
            HomeHDFootReusableView *resV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"HomeHDFootReusableView" forIndexPath:indexPath];
            return resV;
        }
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0 && _popularTypeArr.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, 56);
    } else if (section == 1 && _hdArr.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, 56);
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (section == 0 && _popularTypeArr.count > 0)
    {
        return CGSizeMake(KSCREENWIDTH, 35);
    }
    return CGSizeZero;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (section == 1)
    {
        return 3;
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && self.hdArr.count > 0)
    {
        IGDrawModel *hd = self.hdArr[indexPath.row];
        IGPictureListDetailVC *vc = [[IGPictureListDetailVC alloc] init];
        vc.drawlistId = hd.drawId;
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
